$(function () {
    $('#role-table').datagrid({
        url: ctxPath +"role",
        fit:true,
        fitColumns:true,
        border:false,
        pagination:true,
        striped:true,
        rownumbers:true,
        pageNumber: 1,
        pageSize: 10,
        method:"get",
        columns:[[
            {field:'id',checkbox:true,title:'角色id',width:100,align:"center"},
            {field:'roleName',title:'角色名称',width:100},
            {field:'fcd',title:'创建时间',width:100,align:'center'},
            {field:'lcd',title:'最后更新时间',width:100,align:'center'}
        ]],
        loadFilter: function(role){
                return role.data;
        },
        toolbar:"#div-table-bar"
    });

    //新增按钮
    $("#btn-add").click(function () {
        //打开新增窗口
        $('#div-role').dialog("setTitle","新增角色");
        $('#div-role').dialog("open");
    });
        //关闭按钮
    $("#btn-close").click(function () {
        //打开新增窗口
        $('#div-role').dialog("close");
        $('#form-role').form("clear");
    });
    //提交按钮
    $("#btn-submit").click(function () {
        var method= "post";
        //判读是否为修改
        if ($("#id").val().length>0){
            $("#_method").val("PUT");
       }else
        {
            $("#_method").val("");
        }
        //发起新增请求
        $('#form-role').form('submit',{
            url: ctxPath+"role",
            method: "post",
            onSubmit: function(){
                // 校验表单验证通过
                return $("#form-role").form("validate");

            },
            success:function(data){
                data=$.parseJSON(data);
                if (data.code===20000){
                    //成功消息提示
                    $.messager.show({
                        title:'操作成功',
                        msg: data.mess,
                        timeout:5000,
                        showType:'slide'
                    });
                    //关闭对话框
                    $('#div-role').dialog("close");
                    //刷新表格
                    $('#role-table').datagrid('reload');
                    //清空对话框按钮
                    $('#form-role').form("clear");
                }else{
                    //错误提示
                    $.messager.alert('操作失败','操作失败，请联系管理员!','error');
                }
            }
        });
    });
    $('#div-role').dialog({
        title: '新增角色',
        width: 400,
        height: 250,
        closed: true,
        cache: false,
        modal: true,
        buttons:"#div-buttons",
        onClose: function () {
            $('#form-role').form('clear');
        }
    });
//   角色名称
    $('#roleName').validatebox({
        required: true,
    });
    //权限下拉
    $('#auths').combotree({
        url: ctxPath+"menu",
        method:"get",
        required: true,
        multiple:true,
        cascadeCheck:false,
        onlyLeafCheck:true,
        checkbox:true,
        loadFilter: function(role){
            return role.data;
        },
        onLoadSuccess:function(node,data){
            var _this = this;
            if(data){
                $(data).each(function(){
                    if(this.state == 'closed'){
                        $(_this).tree("expandAll");
                    }
                });
            }
        }
    });
    //修改按钮
    $("#btn-edit").click(function () {
        var rows = $('#role-table').datagrid("getSelections");
        if(rows.length==0){
            $.messager.alert('提示','请选择一条记录!','warning');
        }else if(rows.length>1){
            $.messager.alert('提示','只能选择一条记录!','warning');
        }else{
            //初始化表单数据：根据id查询
            $('#form-role').form('load',ctxPath+'role/'+rows[0].id);
            //打开窗口
            $('#div-role').dialog("setTitle","修改角色");
            $('#div-role').dialog("open");
        }
    })

//删除按钮
    $("#btn-remove").click(function () {
        //至少选择一条
        var rows = $('#role-table').datagrid("getSelections");
        if(rows.length==0){
            $.messager.alert('提示','请选择一条记录!','warning');
        }
        else{
            //确认删除操作
            $.messager.confirm('删除确认', '你确定删除选中记录?', function(r){
                var ids =new Array();
                for (i in rows){
                    ids.push(rows[i].id);
                }
                if (r){
                    //删除
                    var  url=ctxPath + "role";
                    var  p= {
                    _method:"DELETE",
                        ids: ids.join(",")
                    }
                    $.post(url,p,function (data) {
                      if(data.code == 20000){
                          //成功消息提示
                          $.messager.show({
                              title:'操作成功',
                              msg: data.mess,
                              timeout:5000,
                              showType:'slide'
                          });
                          //删除成功刷新表格
                          $('#role-table').datagrid('reload');
                      }else{
                          //删除失败提示
                $.messager.alert('操作失败','操作失败，请联系管理员!','error');
                      }
                    })
                }
            });

        }
    });
});

